package com.lysj.admin.master.bussiness.company.company.service;

import com.lysj.admin.master.bussiness.admin.domain.CompanyProviderBind;
import com.lysj.admin.master.bussiness.admin.domain.Contract;
import com.lysj.admin.master.bussiness.admin.service.CompanyProviderBindService;
import com.lysj.admin.master.bussiness.admin.service.ContractService;
import com.lysj.admin.master.bussiness.company.company.domain.CommissionApply;
import com.lysj.admin.master.bussiness.company.company.domain.CommissionDetail;
import com.lysj.admin.master.bussiness.company.company.mapper.CommissionApplyMapper;
import com.lysj.admin.master.bussiness.company.recharge.domain.CostRecord;
import com.lysj.admin.master.bussiness.company.recharge.service.CostRecordService;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;

/**
 * @author Created by drj on 2019-12-27 16:22
 * @description
 */
@Slf4j
@Service
@Transactional
public class CommissionApplyService extends MyService<CommissionApplyMapper, CommissionApply> {

    @Resource
    private CompanyProviderBindService companyProviderBindService;
    @Resource
    private CommissionDetailService commissionDetailService;
    @Resource
    private ContractService contractService;
    @Resource
    private CostRecordService costRecordService;

    @Transactional
    public void grantOrder(String id, String title, String description, String standard, String contractId) {
        CommissionApply apply = this.getById(id);
        if (apply == null) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "查无该申请");
        }
        if (!CommissionApply.CostStatus.PASS.getCode().equals(apply.getCostStatus())) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "该申请尚未审核通过");
        }
        if (contractId == null) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "请提供合同信息");
        }
        Contract contract = contractService.getById(contractId);
        if (contract == null) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "该合同不存在");
        }
        apply.setTitle(title);
        apply.setDescription(description);
        apply.setStandard(standard);
        apply.setCostStatus(CommissionApply.CostStatus.GRANT.getCode());
        apply.setContractId(contract.getId());
        this.updateById(apply);
        commissionDetailService.updateCommissionStatusByApplyId(id, CommissionDetail.CommissionStatus.NO_RECEIVE);
    }

    public void audit(String id, Integer costStatus, Integer targetType) {
        CommissionApply commissionApply = this.getById(id);
        if (!CommissionApply.CostStatus.AUDITING.getCode().equals(commissionApply.getCostStatus())) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "发放记录审核中状态,请勿重复操作");
        }
        commissionApply.setCostStatus(costStatus);
        commissionApply.setCompleteTime(new Date());
        if (targetType == null) {
            targetType = CommissionApply.TargetType.UNKNOWN.getCode();
        }
        commissionApply.setTargetType(targetType);
        this.saveOrUpdate(commissionApply);
        //如果选择的是驳回
        if (CommissionApply.CostStatus.REJECT.getCode().equals(costStatus)) {
            return;
        }
        //改变用工企业绑定服务商的资产(减少余额,增加发放)
        CompanyProviderBind bind = companyProviderBindService.findByCompanyIdAndServiceProviderId(commissionApply.getCompanyId(), commissionApply.getServiceProviderId());
        bind.setBalance(bind.getBalance().subtract(commissionApply.getActPayPrice()));
        bind.setGrantMoney(bind.getGrantMoney().add(commissionApply.getActPayPrice()));
        companyProviderBindService.saveOrUpdate(bind);

        //生成费用明细
        CostRecord rechargeCostRecord = new CostRecord();
        rechargeCostRecord.setCompanyId(commissionApply.getCompanyId());
        rechargeCostRecord.setCommissionApplyId(commissionApply.getId());
        rechargeCostRecord.setServiceProviderName(commissionApply.getServiceProviderName());
        rechargeCostRecord.setOperationType(CostRecord.OperationType.PAY.getCode());
        rechargeCostRecord.setAmount(commissionApply.getActPayPrice());
        rechargeCostRecord.setOperation(CostRecord.Operation.GRANT.getCode());
        rechargeCostRecord.setRemark("费用发放");
        costRecordService.save(rechargeCostRecord);
    }
}
